package com.lsyonlygoddes.controller;

import com.lsyonlygoddes.util.FileUtils;
import com.lsyonlygoddes.vo.FileVo;
import com.lsyonlygoddes.vo.FolderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:lsyonlygoddes
 * @time:2023/11/23 16:04
 */
@CrossOrigin
@Slf4j
@RestController
public class FileController {
    @GetMapping("/fileList/{pageNum}/{pageSize}")
    public Map<String, Object> fileList(@RequestParam("parentPath")String parentPath, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        List<FileVo> files = FileUtils.files(parentPath);
        map.put("size", files.size());
        int start = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize;
        if (end > files.size()) {
            end = files.size();
        }
        files = files.subList(start, end);
        map.put("data", files);
        return map;
    }

    @GetMapping("/folderList")
    public Map<String, Object> folderList(@RequestParam("parentPath") String parentPath) {
        Map<String, Object> map = new HashMap<>();
        List<FolderVo> folders = FileUtils.folders(parentPath);
        map.put("size", folders.size());

        map.put("data", folders);
        return map;
    }


}
